class Solution1 {
    int[]xx = {1,-1,0,0};
    int[]yy = {0,0,1,-1};
    int n,m;
    public int numIslands(char[][] grid) {
        n = grid.length;
        m = grid[0].length;
        int count = 0;
        for(int i = 0; i<n; i++) {
            for(int j =0; j<m; j++) {
                if(grid[i][j] == '1') {
                    dfs(grid,i,j);
                    count++;
                }
            }
        }
        return count;
    }

    public void dfs(char[][] grid, int x, int y) {
        for(int i = 0; i<4; i++) {
            int dx = x + xx[i];
            int dy = y + yy[i];
            if(dx >= 0 && dx < n && dy >= 0 && dy < m && grid[dx][dy] == '1') {
                grid[dx][dy] = '2';
                dfs(grid,dx,dy);
            }
        }
    }
}

class Solution {
    int[]xx = {1,-1,0,0};
    int[]yy = {0,0,1,-1};
    int n,m;
    int count =0 ;
    int ret = 0;
    public int maxAreaOfIsland(int[][] grid) {
        n = grid.length;
        m = grid[0].length;
        for(int i = 0; i<n; i++) {
            for(int j = 0;j<m; j++) {
                if(grid[i][j] == 1) {
                    grid[i][j] = 2;
                    count++;
                    dfs(grid,i,j);
                    ret = Math.max(ret,count);
                    count = 0;
                }
            }
        }
        return ret;
    }
    public void dfs(int[][] grid, int x, int y) {
        for(int i= 0 ;i<4; i++) {
            int dx = x + xx[i];
            int dy = y + yy[i];
            if(dx >= 0 && dx < n && dy >= 0 && dy < m && grid[dx][dy] == 1) {
                grid[dx][dy] = 2;
                count++;
                dfs(grid, dx, dy);
            }
        }
    }
}
public class Test {
}
